<?php

define('IN_DOUCO', true);
require (dirname(__FILE__) . '/include/init.php');

// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';
$post = $_POST;
if($post){
   $post_data = array(
        'cat_id'  => $post[cat_id], 
        'title'   => $post[title], 
        'content' => $post[content],
        'defined' => $post[defined], 
        'keywords' => $post[keywords], 
        'description' => $post[description],
        'sort' => $post[sort],
    ); 
}
// 赋值给模板
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'article');

//文章列表
if ($rec == 'default') {
    $smarty->assign('ur_here', $_LANG['article']);
    $smarty->assign('action_link', array (
            'text' => $_LANG['article_add'],
            'href' => 'article.php?rec=add' 
    ));
    // 获取参数
    $cat_id = $check->is_number($_REQUEST['cat_id']) ? $_REQUEST['cat_id'] : 0;
    $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
    // 筛选条件
    $where = ' WHERE cat_id IN (' . $cat_id . $dou->dou_child_id('article_category', $cat_id) . ')';
    if ($keyword) {
        $where = $where . " AND title LIKE '%$keyword%'";
        $get = '&keyword=' . $keyword;
    }
    // 分页
    $page = $check->is_number($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $page_url = 'article.php' . ($cat_id ? '?cat_id=' . $cat_id : '');
    $limit = $dou->pager('article', 15, $page, $page_url, $where, $get);
    $res = $dou->get_all("SELECT * FROM " . $dou->table('article') . $where . " ORDER BY id DESC" . $limit);
    foreach($res as $k=>$v){
        $cat_name = $dou->get_one("SELECT cat_name FROM " . $dou->table('article_category') . " WHERE cat_id = '$v[cat_id]'");
        $article_list[$k] = $v;
        $article_list[$k]['cat_name'] = $cat_name;
        $article_list[$k]['add_time'] = date("Y-m-d", $v['add_time']);
    }
    // 首页显示文章数量限制框
    for($i = 1; $i <= $_CFG['home_display_article']; $i++) {
        $sort_bg .= "<li><em></em></li>";
    }
    // 赋值给模板
    $smarty->assign('if_sort', $_SESSION['if_sort']);
    $smarty->assign('sort', get_sort_article());
    $smarty->assign('sort_bg', $sort_bg);
    $smarty->assign('cat_id', $cat_id);
    $smarty->assign('keyword', $keyword);
    $smarty->assign('article_category', $dou->get_category_nolevel('article_category'));
    $smarty->assign('article_list', $article_list);
    $smarty->display('article.htm');
} 

//文章添加
elseif ($rec == 'add') {
    $smarty->assign('ur_here', $_LANG['article_add']);
    $smarty->assign('action_link', array (
            'text' => $_LANG['article'],
            'href' => 'article.php' 
    ));
    // 格式化自定义参数，并存到数组$article，文章编辑页面中调用文章详情也是用数组$article，
    if ($_DEFINED['article']) {
        $defined = explode(',', $_DEFINED['article']);
        foreach ($defined as $row) {
            $defined_article .= $row . "：\n";
        }
        $article['defined'] = trim($defined_article);
        $article['defined_count'] = count(explode("\n", $article['defined'])) * 2;
    }
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('form_action', 'insert');
    $smarty->assign('article_category', $dou->get_category_nolevel('article_category'));
    $smarty->assign('article', $article);
    $smarty->display('article.htm');
}
elseif ($rec == 'insert') {
    // 验证标题
    if (empty($post['title'])) $dou->dou_msg($_LANG['article_name'] . $_LANG['is_empty']);
    // 图片上传
    $image = upload_image('image','article');
    // CSRF防御令牌验证
    $firewall->check_token($post['token']);
    //插入数据库
    $post_data[id] = NULL;
    $post_data[add_time] = time();
    $post_data[defined] = str_replace("\r\n", ',', $post['defined']);
    $post_data[image] = $image;
    $dou->query( $dou->insert_sql('article', $post_data) );
    $dou->create_admin_log($_LANG['article_add'] . ': ' . $post['title']);
    $dou->dou_header('article.php');
}

//文章编辑
elseif ($rec == 'edit') {
    $smarty->assign('ur_here', $_LANG['article_edit']);
    $smarty->assign('action_link', array ('text' => $_LANG['article'],'href' => 'article.php' ));
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : '';
    $query = $dou->select($dou->table('article'), '*', '`id` = \'' . $id . '\'');
    $article = $dou->fetch_array($query);
    // 格式化自定义参数
    if ($_DEFINED['article'] || $article['defined']) {
        $defined = explode(',', $_DEFINED['article']);
        foreach ($defined as $row) {
            $defined_article .= $row . "：\n";
        }
        // 如果文章中已经写入自定义参数则调用已有的
        $article['defined'] = $article['defined'] ? str_replace(",", "\n", $article['defined']) : trim($defined_article);
        $article['defined_count'] = count(explode("\n", $article['defined'])) * 2;
    }
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('form_action', 'update');
    $smarty->assign('article_category', $dou->get_category_nolevel('article_category'));
    $smarty->assign('article', $article);
    $smarty->display('article.htm');
} 
elseif ($rec == 'update') {
    // 验证标题
    if (empty($post['title'])) $dou->dou_msg($_LANG['article_name'] . $_LANG['is_empty']);
    // CSRF防御令牌验证
    $firewall->check_token($post['token']);
    // 图片上传
    $image = upload_image('image','article');
    //更新数据库
    if(!empty($image)) $post_data[image] = $image;
    $dou->query( $dou->update_sql('article', $post_data, "id='$post[id]'") );
    $dou->create_admin_log($_LANG['article_edit'] . ': ' . $post['title']);
    $dou->dou_header('article.php');
} 
//文章删除
elseif ($rec == 'del') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'article.php');
    $title = $dou->get_one("SELECT title FROM " . $dou->table('article') . " WHERE id = '$id'");
    if (isset($post['confirm']) ? $post['confirm'] : '') {
        // 删除相应商品图片
        $image = $dou->get_one("SELECT image FROM " . $dou->table('article') . " WHERE id = '$id'");
        $dou->del_image($image);
        $dou->create_admin_log($_LANG['article_del'] . ': ' . $title);
        $dou->delete($dou->table('article'), "id = $id", 'article.php');
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $title, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'article.php', '', '30', "article.php?rec=del&id=$id");
    }
}

//首页商品筛选
elseif ($rec == 'sort') {
    $_SESSION['if_sort'] = $_SESSION['if_sort'] ? false : true;
    // 跳转到上一页面
    $dou->dou_header($_SERVER['HTTP_REFERER']);
} 
//设为首页显示商品
elseif ($rec == 'set_sort') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'article.php');
    $max_sort = $dou->get_one("SELECT sort FROM " . $dou->table('article') . " ORDER BY sort DESC");
    $new_sort = $max_sort + 1;
    $dou->query("UPDATE " . $dou->table('article') . " SET sort = '$new_sort' WHERE id = '$id'");
    $dou->dou_header($_SERVER['HTTP_REFERER']);
} 
//取消首页显示商品
elseif ($rec == 'del_sort') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'article.php');
    $dou->query("UPDATE " . $dou->table('article') . " SET sort = '' WHERE id = '$id'");
    $dou->dou_header($_SERVER['HTTP_REFERER']);
} 
//批量操作选择
elseif ($rec == 'action') {
    if (is_array($post['checkbox'])) {
        if ($post['action'] == 'del_all') {
            // 批量文章删除
            $dou->del_all('article', $post['checkbox'], 'id', 'image');
        } elseif ($post['action'] == 'category_move') {
            // 批量移动分类
            $dou->category_move('article', $post['checkbox'], $post['new_cat_id']);
        } else {
            $dou->dou_msg($_LANG['select_empty']);
        }
    } else {
        $dou->dou_msg($_LANG['article_select_empty']);
    }
}


/**
 * +----------------------------------------------------------
 * 获取首页显示文章
 * +----------------------------------------------------------
 */
function get_sort_article() {
    $limit = $GLOBALS['_DISPLAY']['home_article'] ? ' LIMIT ' . $GLOBALS['_DISPLAY']['home_article'] : '';
    $sql = "SELECT id, title FROM " . $GLOBALS['dou']->table('article') . " WHERE sort > 0 ORDER BY sort DESC" . $limit;
    $query = $GLOBALS['dou']->query($sql);
    while ($row = $GLOBALS['dou']->fetch_array($query)) {
        $sort[] = array (
                "id" => $row['id'],
                "title" => $row['title'] 
        );
    }
    return $sort;
}
?>